package com.wonders.frame.service;

import com.gcr.server.response.Response;
import com.wonders.frame.common.Enum.CommonCodeEnum;
import com.wonders.frame.common.Enum.RemovedCodeEnum;
import com.wonders.frame.entity.AuthDic;
import com.wonders.frame.entity.AuthDicType;
import com.wonders.frame.entity.AuthOrga;
import com.wonders.frame.entity.Tree.SelectTreeNodeData;
import com.wonders.frame.entity.form.AuthDicForm;
import com.wonders.frame.repository.AuthDicRepository;
import com.wonders.frame.repository.AuthDicTypeRepository;
import com.wonders.frame.repository.core.CoreRepository;
import com.wonders.frame.service.Core.CoreCurdService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class AuthDicService extends CoreCurdService<AuthDic> {
    Logger log = LoggerFactory.getLogger(AuthDicService.class);

    @Autowired
    private AuthDicRepository authDicRepository;

    @Override
    protected CoreRepository<AuthDic> getRepository() {
        return authDicRepository;
    }

    @Autowired
    private AuthDicTypeRepository authDicTypeRepository;



    /**
     * 验证
     * @param code
     * @param dicType
     * @return
     */
    public Integer checkUnique(String id, String code, String name, String dicType){
        List<AuthDic> codeList = authDicRepository.findByCodeAndDicTypeAndRemoved(code, dicType, RemovedCodeEnum.AVAILABLE.getCode());
        List<AuthDic> nameList = authDicRepository.findByNameAndDicTypeAndRemoved(name, dicType, RemovedCodeEnum.AVAILABLE.getCode());

        if (Strings.isNotEmpty(id)) {
            AuthDic authDicOld = authDicRepository.findById(id).get();
            if (!Objects.equals(code, authDicOld.getCode()) && !CollectionUtils.isEmpty(codeList)) {
                //code重复
                return 1;
            }

            if (!Objects.equals(name, authDicOld.getName())&&!CollectionUtils.isEmpty(nameList)) {
                //name重复
                return 2;
            }

            return 0;
        }


        if(!CollectionUtils.isEmpty(codeList)){
            //code重复
            return 1;
        }

        if(!CollectionUtils.isEmpty(nameList)){
            //name重复
            return 2;
        }

        return 0;
    }


    /**
     * 根据dicType获取字典列表
     * @param dicType
     * @return
     */
    public List<AuthDic> getAuthDicListByDicType(String dicType){
        if(StringUtils.isNotEmpty(dicType)){
            return authDicRepository.getByDicType(dicType);
        }
        return null;
    }
    /**
     * 逻辑删除
     */
    public CommonCodeEnum deleteAuthDicById(String id){

        try {
            AuthDic authDic = authDicRepository.findById(id).get();
            authDic.setRemoved(1);
            authDicRepository.save(authDic);
            return CommonCodeEnum.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return CommonCodeEnum.ERROR;
        }
    }
/*    public void deleteDicByDicTypeId(String dicTypeId) {
        try {
            List<String> authDics = authDicRepository.findByDicTypeId(dicTypeId);
            for (String authDic : authDics) {
                deleteAuthDicById(authDic);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }*/


    /**
     * 根据 字典类型查询，获得字典树
     * @param dicType
     * @return
     */
/*    public List<SelectTreeNodeData> getAuthDicByDicType(String dicType){
        List<SelectTreeNodeData> nodes = new ArrayList<SelectTreeNodeData>();
        List<SelectTreeNodeData> finallyNodes = new ArrayList<SelectTreeNodeData>();
        List<AuthDic> authDics= authDicRepository.getByDicType(dicType);
        //自身封装
        for(AuthDic authDic:authDics){
            nodes.add(getAuthDicNode(authDic));
        }
        //父层dicType封装：需要在nodes 上在封装一层dicType表,
        List<AuthDicType> parent= authDicTypeRepository.findAuthDicTypeByCode(dicType);
        SelectTreeNodeData selectTreeNodeData = new SelectTreeNodeData();
        if(parent !=null && parent.size()>0){
            AuthDicType authDicType = parent.get(0);
            selectTreeNodeData.setTitle(authDicType.getName());
            selectTreeNodeData.setValue(authDicType.getId());
            selectTreeNodeData.setKey(authDicType.getId());
            selectTreeNodeData.setChildren(nodes);
            finallyNodes.add(selectTreeNodeData);
        }
        return finallyNodes;

    }
    *//**
     * 递归调用初始化字典basic树
     *//*
    public SelectTreeNodeData getAuthDicNode(AuthDic authDic){
        SelectTreeNodeData nodeData = new SelectTreeNodeData();
        nodeData.setKey(authDic.getId());
        nodeData.setValue(authDic.getId());
        nodeData.setTitle(authDic.getName());
        return nodeData;
    }*/

    public List<SelectTreeNodeData> getAuthDicByDicType(String dicType){
        List<SelectTreeNodeData> nodes = new ArrayList<SelectTreeNodeData>();
        List<AuthDic> authDics= authDicRepository.getByDicType(dicType);
        List<String> dicIds=authDicRepository.getIdsByDicType(dicType);
        //自身封装
        for(AuthDic authDic:authDics) {
            if(authDic.getDicParentId()==null){
                nodes.add(getAuthDicNode1(authDic));
            }
        }
        return nodes;

    }

    private SelectTreeNodeData getAuthDicNode1(AuthDic authDic){
        SelectTreeNodeData nodeData = new SelectTreeNodeData();
        nodeData.setKey(authDic.getId());
        nodeData.setValue(authDic.getId());
        nodeData.setTitle(authDic.getName());
        if(authDic.getAuthDicChildren() != null){
            List<SelectTreeNodeData> nodes = new ArrayList<SelectTreeNodeData>();
            for(AuthDic o : authDic.getAuthDicChildren()){
                if(o.getRemoved()==0){
                    nodes.add(getAuthDicNode1(o));
                }
            }
            nodeData.setChildren(nodes);
        }
        return nodeData;
    }


    /**
     * 根据 字典类型查询，获得全部system字典值
     * @return
     */
    public List<AuthDic> getAllDicValues(){
        return  authDicRepository.getAllDic();
    }


    /**
     * 获取所有字典
     * @return
     */
    public List<AuthDic> getAllAuthDic(){
        return authDicRepository.getAllAuthDic();
    }

    /**
     * 新增字典
     */
    public AuthDic saveAuthDic(AuthDicForm authDicForm){
        AuthDic authDic = new AuthDic();
        BeanUtils.copyProperties(authDicForm,authDic);
        if(StringUtils.isEmpty(authDic.getId())){
            log.debug("new AuthDic");
            authDic.setRemoved(RemovedCodeEnum.AVAILABLE.getCode());
            authDic.setCrtDate(new Date());
        }else {
            log.debug("modify authDicType");
            authDic.setUpdDate(new Date());
        }
        List<String> dicIds = authDicRepository.getAllIds();
        if(dicIds.contains(authDic.getParentId())){
            authDic.setDicParentId(authDic.getParentId());
            authDic.setParentId(null);
        }
        return authDicRepository.save(authDic);
    }



    public AuthDic getAuthDicByCodeAndDicType(String code,String dicType){
        return authDicRepository.getByCodeAndAndDicTypeAndRemoved(code,dicType,RemovedCodeEnum.AVAILABLE.getCode());
    }



}
